package com.yunshang.shoppingmall.shopping.OrderController;

import com.yunshang.shoppingmall.common.JacksonHelper;
import com.yunshang.shoppingmall.entity.Logistics;
import com.yunshang.shoppingmall.entity.pojo.NoticeRequest;
import com.yunshang.shoppingmall.entity.pojo.NoticeResponse;
import com.yunshang.shoppingmall.entity.pojo.Result;
import com.yunshang.shoppingmall.service.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RequestMapping("/wap")
@Controller
public class LogisticsCallBackController extends HttpServlet {

    @Autowired
    LogisticsService logisticsService;
    private static final long serialVersionUID = 1L;

    public LogisticsCallBackController() {
        super();
    }
    @ResponseBody
    @RequestMapping("/doPostLogistics")
    public void doPostLogistics(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        System.out.println("物流回调");
        NoticeResponse resp = new NoticeResponse();
        resp.setResult(false);
        resp.setReturnCode("500");
        resp.setMessage("保存失败");
        try {
            String param = request.getParameter("param");
            //param="{\"status\":\"shutdown\",\"billstatus\":\"check\",\"message\":\"\",\"autoCheck\":\"0\",\"comOld\":\"\",\"comNew\":\"\",\"lastResult\":{\"message\":\"ok\",\"nu\":\"51164633704467\",\"ischeck\":\"1\",\"condition\":\"F00\",\"com\":\"huitongkuaidi\",\"status\":\"200\",\"state\":\"3\",\"data\":[{\"time\":\"2018-09-09 17:13:59\",\"ftime\":\"2018-09-09 17:13:59\",\"context\":\"汕头市|汕头市【澄海】，草签 已签收\"},{\"time\":\"2018-09-09 15:05:46\",\"ftime\":\"2018-09-09 15:05:46\",\"context\":\"汕头市|汕头市【澄海】，【许林发/13288080783】正在派件\"},{\"time\":\"2018-09-09 13:37:37\",\"ftime\":\"2018-09-09 13:37:37\",\"context\":\"汕头市|汕头市【澄海】，正发往【澄海001】\"},{\"time\":\"2018-09-09 10:31:53\",\"ftime\":\"2018-09-09 10:31:53\",\"context\":\"汕头市|汕头市【汕头转运中心】，正发往【澄海】\"},{\"time\":\"2018-09-09 09:56:43\",\"ftime\":\"2018-09-09 09:56:43\",\"context\":\"汕头市|到汕头市【汕头转运中心】\"},{\"time\":\"2018-09-09 02:18:48\",\"ftime\":\"2018-09-09 02:18:48\",\"context\":\"广州市|广州市【广州转运中心】，正发往【汕头转运中心】\"},{\"time\":\"2018-09-08 17:58:36\",\"ftime\":\"2018-09-08 17:58:36\",\"context\":\"广州市|到广州市【广州转运中心】\"},{\"time\":\"2018-09-08 01:33:37\",\"ftime\":\"2018-09-08 01:33:37\",\"context\":\"贵阳市|贵阳市【贵阳转运中心】，正发往【广州转运中心】\"},{\"time\":\"2018-09-07 21:07:24\",\"ftime\":\"2018-09-07 21:07:24\",\"context\":\"贵阳市|到贵阳市【贵阳转运中心】\"},{\"time\":\"2018-09-07 15:40:12\",\"ftime\":\"2018-09-07 15:40:12\",\"context\":\"贵阳市|贵阳市【贵阳观山湖二部】，【邹丽霞/18302589430】已揽收\"}]}}";
            NoticeRequest nReq = JacksonHelper.fromJSON(param,NoticeRequest.class);

            Result result = nReq.getLastResult();
            // 处理快递结果
             String info=JacksonHelper.toJSON(result.getData());
            Logistics logistics=logisticsService.selectByNo(result.getNu());
            if(logistics!=null){
                logistics.setLogisticsInfo(info);
                Integer res=logisticsService.updateByPrimaryKeyWithBLOBs(logistics);
                if (res>=1){
                    resp.setResult(true);
                    resp.setReturnCode("200");
                    response.getWriter().print(JacksonHelper.toJSON(resp)); //这里必须返回，否则认为失败，过30分钟又会重复推送。
                    System.out.println("A1:"+JacksonHelper.toJSON(resp));
                }else {
                    response.getWriter().print(JacksonHelper.toJSON(resp));
                    System.out.println("B1:"+JacksonHelper.toJSON(resp));
                }
            }else{
                Logistics logistics1=new Logistics();
                logistics1.setLogisticsNo(result.getNu());
                logistics1.setLogisticsCompanyname(result.getCom());
                logistics1.setLogisticsInfo(JacksonHelper.toJSON(result.getData()));
                logistics1.setLogisticsCompanyname("");
                Integer res=logisticsService.insert(logistics1);
                System.out.println(res+"----------------");
                if (res>=1){
                    resp.setResult(true);
                    resp.setReturnCode("200");
                    response.getWriter().print(JacksonHelper.toJSON(resp)); //这里必须返回，否则认为失败，过30分钟又会重复推送。
                }else {
                    response.getWriter().print(JacksonHelper.toJSON(resp));
                    System.out.println("B2:"+JacksonHelper.toJSON(resp));
                }
            }
        } catch (Exception e) {
            resp.setMessage("保存失败" + e.getMessage());
            response.getWriter().print(JacksonHelper.toJSON(resp));//保存失败，服务端等30分钟会重复推送。
            System.out.println("C:"+JacksonHelper.toJSON(resp));
        }
    }
}
